<?php
namespace app\module\admin\controllers;
/**
 * 功能描述:后台数据备份+还原等操作
 * @Author:冰封  QQ:574578944
 * @date:2012-09-03
 */
use Yii;
use yii\web\Controller;
use app\lib\mysql_back;
class DataBackController extends Controller
{
    private $config;
    private $mr;
    public $layout=false;//设置当前默认布局文件为假

    public function init(){//初始化相关属性
        $connect_str = Yii::$app->db->dsn;
        //mysql:host=127.0.0.1;dbname=biz_weixin
        //$re_str = explode('=', implode('=', explode(';', $connect_str['dbname'])));//取得数据库IP,数据库名
        $tmp=explode(';',$connect_str);
        $dbname=explode('=',$tmp[1])[1];
        $host=explode('=',$tmp[0])[1];
       //取得数据库IP,数据库名
        $this->config = array( //设置参数
            'host' =>$host,
            'dbname'=> $dbname,
            'port' => 3306,
            'username' => Yii::$app->db->username,
            'userPassword' => Yii::$app->db->password,
            'dbprefix' => "",
            'charset' => Yii::$app->db->charset,
            'path' => './backup/',    //定义备份文件的路径
            'isCompress' => 1,             //是否开启gzip压缩{0为不开启1为开启}
            'isDownload' => 0               //压缩完成后是否自动下载{0为不自动1为自动}
        );
        $this->mr = new mysql_back($this->config);
    }

    /**
     * @显示已备份的数据列表
     */
    public function actionShow_data($msg=""){
        $path = $this->config['path'];
        $fileArr = $this->MyScandir($path);
        $list = array();
        foreach ($fileArr as $key => $value){
            if($key > 1){
                //获取文件创建时间
                $fileTime = date('Y-m-d H:i',filemtime($path . $value));
                $fileSize = filesize($path.$value)/1024;
                //获取文件大小
                $fileSize = $fileSize < 1024 ? number_format($fileSize,2).' KB':
                    number_format($fileSize/1024,2).' MB';
                //构建列表数组
                $list[]=array(
                    'name' => $value,
                    'time' => $fileTime,
                    'size' => $fileSize
                );
            }
        }

        return $this->render('data_back',array('data'=>$list,'msg'=>$msg));
    }
    public function actionIndex(){

        $connect_str = Yii::$app->db->dsn;
        //mysql:host=127.0.0.1;dbname=biz_weixin
        //$re_str = explode('=', implode('=', explode(';', $connect_str['dbname'])));//取得数据库IP,数据库名
        $tmp=explode(';',$connect_str);
        $dbname=explode('=',$tmp[1])[1];
        $host=explode('=',$tmp[0])[1];
        echo $host;

        //  return  $this->render("index");
    }
    /**
     * @备份数据库
     */
    public function actionBackup(){
        $this->mr->setDBName($this->config['dbname']);
        if($this->mr->backup()){
            die;
            messages::show_msg($this->createUrl('databack/show_data'), '数据库备份成功！!');
        }else{
            messages::show_msg($this->createUrl('databack/show_data'), '数据库备份失败！!');
        }
    }
    /**
     * @删除数据备份
     */
    public function actionDelete_back(){
        if(unlink($this->config['path'] . $_GET['file'])){
            messages::show_msg($this->createUrl('databack/show_data'), '删除备份成功！!');
        }else{
            messages::show_msg($this->createUrl('databack/show_data'), '删除备份失败！!');
        }
    }

    /**
     * @下载备份文件
     */
    public function actionDownloadbak(){
        $this->download($this->config['path'] . $_GET['file']);
    }
    /**
     * @还原数据库
     */
    public function actionrecover(){
        $this->mr->setDBName($this->config['dbname']);
        if($this->mr->recover($_GET['file'])){
            messages::show_msg($this->createUrl('databack/show_data'), '数据还原成功！!');
        }else{
            messages::show_msg($this->createUrl('databack/show_data'), '数据还原失败！!');
        }
    }

    /**
     * @获取目录下文件数组
     */
    public function MyScandir($FilePath='./',$Order=0){
        $FilePath = opendir($FilePath);
        while($filename = readdir($FilePath)) {
            $fileArr[] = $filename;
        }
        $Order == 0 ? sort($fileArr) : rsort($fileArr);
        return $fileArr;
    }
    /**
     * @公共下载方法
     */
    public function download($filename){
        ob_end_clean();
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-streamextension');
        header('Content-Length: '.filesize($filename));
        header('Content-Disposition: attachment; filename='.basename($filename));
        readfile($filename);
    }
}